/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/5
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.Controller;

import com.alibaba.fastjson.JSON;
import com.it1997.it1997admin.pojo.Permission;
import com.it1997.it1997admin.pojo.Role;
import com.it1997.it1997admin.pojo.User;
import com.it1997.it1997admin.service.PermissionService;
import com.it1997.it1997admin.utils.ResultUtil;
import com.it1997.it1997admin.vo.PermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Log4j2
@Api(tags = "权限接口")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/getMenu")
    @ResponseBody
    @ApiOperation(value = "获取菜单列表")
    public String loadPermission(@AuthenticationPrincipal User user){
        Map<String, Object> map = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);
        List <Permission> result=null;
           for (Role role:user.getUserRoles()){
               //                   //查询出根权限集合
                   log.info("角色id："+role.getId());
                   List<Permission> rootPermissionList=permissionService.getChildPermissions(0,role.getId());
                   //查询所有的权限集合
                   result=permissionService.getPermissionList(rootPermissionList,role.getId());
        }
        home.put("title","首页");
        home.put("href","");//控制器路由,自行定义
        logo.put("title","后台管理系统");
        logo.put("image","/layuimini/images/logo.png");//静态资源文件路径,可使用默认的logo.png
        logo.put("href","#");
        map.put("homeInfo", home);
        map.put("logoInfo", logo);
        map.put("menuInfo", result);
        return JSON.toJSONString(map);
    }
    @GetMapping("/getAllPermission")
    @ResponseBody
    @ApiOperation(value = "获取权限列表")
    public String getAllPermission(){
        Map<String, Object> map = new HashMap<>(16);
        //查询根权限

        List<PermissionVo> rootList=permissionService.getChildPermissions(0);
        log.info("根权限："+rootList);
        //查询子权限
        List<PermissionVo> permissionVoList =permissionService.getAllPermission(rootList);
        map.put("data",permissionVoList);
        return JSON.toJSONString(permissionVoList);
    }
}
